 using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[Controller]")]
    public class CommentController :ControllerBase
    {
        private readonly IRepostisory<Comment> _commentRepostisory;
        public CommentController(IRepostisory<Comment> commentRepostisory){
            _commentRepostisory = commentRepostisory;
        }
        /// <summary>
        /// 发布评论
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpPost,Route("toComment")]
        public dynamic CommentMsg(ComMsg msg){
            //文章id
            var ArticleId = msg.ArticleId;
            //用户id
            var UserId = msg.UserId;
            //对文章id
            var ToCommentId = msg.ToCommentId;
            //评论内容   
            var Content = msg.Content;
            var res = new Comment {
                ArticleId = ArticleId,
                FromUserId = UserId,
                ToCommentId = ToCommentId,
                Content = Content
            };
            _commentRepostisory.Insert(res);
            return new{
                code = 200,
                data = res,
                msg = "评论成功"
            };
        }

        
        [HttpGet,Route("{id}/getComment")]
        public dynamic GetCommentMsg(int id){

            var ArticleId = id;
            var CommentTable = _commentRepostisory.Table;
            var ArticleComment = CommentTable.Where(x=>x.ArticleId ==ArticleId).ToList();
            return new {
                code = 200,
                data = ArticleComment,
                mag = "获取成功"
            };
        }
    }
}